package com.hs.conc.thread;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

/**
 * @Author: songhao
 * @Date: 2021/8/28
 * @Description:
 * 方法：通过一个BlockingQueue阻塞队列实现
 */
public class MyThread7 extends Thread{

    //一个阻塞队列
    static BlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(1);

    public static void main(String[] args) throws Exception{
        long start=System.currentTimeMillis();

        //创建一个线程，并进行阻塞
        MyThread7 myThread = new MyThread7();
        myThread.start();

        int result = queue.take();

        // 确保  拿到result 并输出
        System.out.println("计算结果为：" + result);
        System.out.println("使用时间："+ (System.currentTimeMillis()-start) + " ms");
    }

    @Override
    public void run() {
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        try {
            queue.put(sum());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private static int sum() {
        return fibo(36);
    }

    private static int fibo(int a) {
        if ( a < 2)
            return 1;
        return fibo(a-1) + fibo(a-2);
    }
}
